package org.example.service.impl;

import com.baidu.fsg.uid.UidGenerator;
import org.apache.shardingsphere.transaction.annotation.ShardingTransactionType;
import org.example.mapper.ProjectMapper;
import org.example.service.TestService;
import org.example.vo.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Autowired
    private UidGenerator uidGenerator;

    @Override
    @Transactional
    public int addDatas(List<Map<String, Object>> datas) {
        String sql = "insert into test_user(age, city, money, id) values(:age, :city, :money, :id)";
        datas.forEach(d->{
            d.put("id", uidGenerator.getUID());
        });
        SqlParameterSource[] datasArray = SqlParameterSourceUtils.createBatch(datas);
        namedParameterJdbcTemplate.batchUpdate(sql, datasArray);
        return datas.size();
    }

    @Override
    @Transactional
    public int deleteDatas() {
        String sql = "delete from test_user";
        return jdbcTemplate.update(sql);
    }

    @Override
    public long countDatas() {
        String sql = "select count(1) from test_user";
        Long count = jdbcTemplate.queryForObject(sql, Long.class);
        return count;
    }

    @Override
    public List<Map<String, Object>> getDatas(int start, int limit) {
        String sql = "select * from test_user order by age limit ?,?";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql, (start-1)*limit, limit);
        return mapList;
    }

    @Autowired
    private ProjectMapper projectMapper;

    @Override
//    @Transactional
    @ShardingTransactionType
    public void batchInsertProjects(String namePrefix, int stepLength, String[] uids) {
        //int stepLength = 10000;
        //List<Project> projectList = new ArrayList<>(stepLength*uids.length);
        for (int j = 0; j < uids.length; j++) {
            String uid = uids[j];
            for (int i = j*stepLength; i < (j+1)*stepLength; i++) {
                Project project = new Project();
                project.setId(uidGenerator.getUID());
                project.setName(namePrefix + (i+1));
                project.setChargeUserId(uid);
                project.setCreateTime(new Date());
                project.setSortOrder(Double.valueOf(""+(i+1)));
               // projectList.add(project);
                projectMapper.insert(project);
                if(i==(stepLength*3)-4){
                    int k = 10/0;
                    System.out.println(k);
                }
            }
        }
        //projectMapper.insert(projectList);
    }
}
